package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import util.DBUtil;

import dao.CityDao;
import entity.City;

public class CityDaoJdbcImpl implements CityDao {

	@Override
	public List<City> findByParent(int parentId) throws Exception {
		String sql = "select * from city where parent=?";
		Connection con = DBUtil.openInThread();
		PreparedStatement pst = con.prepareStatement(sql);
		pst.setInt(1, parentId);
		ResultSet rs = pst.executeQuery();
		List<City> list = new ArrayList<City>();
		while(rs.next()){
			City city = new City();
			city.setId(rs.getInt("id"));
			city.setCityname(rs.getString("cityname"));
			city.setParent(rs.getInt("parent"));
			list.add(city);
		}
		DBUtil.close(null, pst, rs);
		DBUtil.closeInThread();
		return list;
	}

}
